﻿using System;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using RaGE.Properties;

namespace RaGE
{
    public partial class ConfigWindow : Form
    {
        public ConfigWindow()
        {
            InitializeComponent();
        }

        private void AddNewClick(object sender, EventArgs e)
        {
            Array.Resize(ref listNames, listNames.Length + 1);
            listNames[listNames.Length - 1] = "< LIST >";

            Array.Resize(ref listOffsets, listOffsets.Length + 1);
            listOffsets[listOffsets.Length - 1] = "0";

            Array.Resize(ref fieldNames, fieldNames.Length + 1);
            fieldNames[fieldNames.Length - 1] = new[] { "< NAME >" };

            Array.Resize(ref fieldTypes, fieldTypes.Length + 1);
            fieldTypes[fieldTypes.Length - 1] = new[] { "< TYPE >" };

            comboBox_ConfigLists.Items.Add("[" + (listNames.Length - 1) + "]: " + listNames[listNames.Length - 1]);
            comboBox_ConfigLists.SelectedIndex = comboBox_ConfigLists.Items.Count - 1;
        }

        private void DeleteClick(object sender, EventArgs e)
        {
            if (comboBox_ConfigLists.SelectedIndex <= -1) return;

            var index = comboBox_ConfigLists.SelectedIndex;

            var itemp = new string[listOffsets.Length - 1];

            Array.Copy(listOffsets, 0, itemp, 0, index);
            Array.Copy(listOffsets, index + 1, itemp, index, listOffsets.Length - 1 - index);
            listOffsets = itemp;

            var stemp = new string[listNames.Length - 1];

            Array.Copy(listNames, 0, stemp, 0, index);
            Array.Copy(listNames, index + 1, stemp, index, listNames.Length - 1 - index);
            listNames = stemp;

            var astemp = new string[fieldNames.Length - 1][];

            Array.Copy(fieldNames, 0, astemp, 0, index);
            Array.Copy(fieldNames, index + 1, astemp, index, fieldNames.Length - 1 - index);
            fieldNames = astemp;

            astemp = new string[fieldTypes.Length - 1][];

            Array.Copy(fieldTypes, 0, astemp, 0, index);
            Array.Copy(fieldTypes, index + 1, astemp, index, fieldTypes.Length - 1 - index);
            fieldTypes = astemp;

            dataGridView_item.Rows.Clear();
            comboBox_ConfigLists.Items.Clear();

            for (var i = 0; i < listNames.Length; i++)
                comboBox_ConfigLists.Items.Add("[" + i + "]: " + listNames[i]);

            index--;

            if (index < 0 && comboBox_ConfigLists.Items.Count > 0)
                index++;

            comboBox_ConfigLists.SelectedIndex = index;
        }

        private void LoadClick(object sender, EventArgs e)
        {
            var cLoad = new OpenFileDialog();
            cLoad.InitialDirectory = Application.StartupPath + "\\configs";
            cLoad.Filter = Resources.ELCFGFilter;

            if (cLoad.ShowDialog() != DialogResult.OK || !File.Exists(cLoad.FileName)) return;

            var sr = new StreamReader(cLoad.FileName);

            comboBox_ConfigLists.Items.Clear();

            var listCount = Convert.ToInt32(sr.ReadLine());

            listNames = new string[listCount];
            listOffsets = new string[listCount];
            fieldNames = new string[listCount][];
            fieldTypes = new string[listCount][];

            try
            {
                conversationListIndex = Convert.ToInt32(sr.ReadLine());
            }
            catch
            {
                conversationListIndex = 58;
            }

            textBox_conversationListIndex.Text = conversationListIndex.ToString(CultureInfo.InvariantCulture);

            for (var i = 0; i < listCount; i++)
            {
                string line;

                while ((line = sr.ReadLine()) == "")
                {
                }

                comboBox_ConfigLists.Items.Add("[" + i + "]: " + line);
                listNames[i] = line;
                listOffsets[i] = sr.ReadLine();

                var readLine = sr.ReadLine();
                if (readLine != null) fieldNames[i] = readLine.Split(new[] { ';' });

                var s = sr.ReadLine();
                if (s != null) fieldTypes[i] = s.Split(new[] { ';' });
            }
            sr.Close();
        }

        private void SaveClick(object sender, EventArgs e)
        {
            var cSave = new SaveFileDialog();
            cSave.InitialDirectory = Application.StartupPath + "\\configs";
            cSave.Filter = Resources.ELCFGFilter;

            if (cSave.ShowDialog() != DialogResult.OK || cSave.FileName == "") return;

            var sw = new StreamWriter(cSave.FileName);
            var listCount = listNames.Length;

            sw.WriteLine(listCount);
            sw.WriteLine(conversationListIndex);

            for (var i = 0; i < listCount; i++)
            {
                sw.WriteLine();
                sw.WriteLine(listNames[i]);
                sw.WriteLine(listOffsets[i]);
                sw.WriteLine(string.Join(";", fieldNames[i]));
                sw.WriteLine(string.Join(";", fieldTypes[i]));
            }

            sw.Close();
        }

        private void ChangeConversationListIndex(object sender, EventArgs e)
        {
            conversationListIndex = Convert.ToInt32(textBox_conversationListIndex.Text);
        }

        private void ChangeList(object sender, EventArgs e)
        {
            if (comboBox_ConfigLists.SelectedIndex <= -1) return;

            textBox_listName.Text = listNames[comboBox_ConfigLists.SelectedIndex];
            textBox_offset.Text = listOffsets[comboBox_ConfigLists.SelectedIndex];
            dataGridView_item.Rows.Clear();

            for (var i = 0; i < fieldNames[comboBox_ConfigLists.SelectedIndex].Length; i++)
            {
                dataGridView_item.Rows.Add(new object[] { fieldNames[comboBox_ConfigLists.SelectedIndex][i],
                    fieldTypes[comboBox_ConfigLists.SelectedIndex][i] });
                dataGridView_item.Rows[i].HeaderCell.Value = i.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void ChangeListName(object sender, EventArgs e)
        {
            if (comboBox_ConfigLists.SelectedIndex <= -1) return;

            listNames[comboBox_ConfigLists.SelectedIndex] = textBox_listName.Text;
            comboBox_ConfigLists.Items[comboBox_ConfigLists.SelectedIndex] =
                "[" + comboBox_ConfigLists.SelectedIndex + "]: " + textBox_listName.Text;
        }

        private void ChangeListOffset(object sender, EventArgs e)
        {
            if (comboBox_ConfigLists.SelectedIndex > -1)
                listOffsets[comboBox_ConfigLists.SelectedIndex] = textBox_offset.Text;
        }

        private void ChangeRow(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (fieldNames != null && e.ColumnIndex == 0)
                    fieldNames[comboBox_ConfigLists.SelectedIndex][e.RowIndex] =
                        dataGridView_item[e.ColumnIndex, e.RowIndex].Value.ToString();

                if (fieldTypes != null && e.ColumnIndex == 1)
                    fieldTypes[comboBox_ConfigLists.SelectedIndex][e.RowIndex] =
                        dataGridView_item[e.ColumnIndex, e.RowIndex].Value.ToString();
            }
            catch
            {
                MessageBox.Show(Resources.ErrChanging);
            }
        }

        private void AddRow(object sender, EventArgs e)
        {
            if (comboBox_ConfigLists.SelectedIndex <= -1) return;

            var temp = new string[fieldNames[comboBox_ConfigLists.SelectedIndex].Length + 1];

            Array.Copy(fieldNames[comboBox_ConfigLists.SelectedIndex], 0, temp, 0, dataGridView_item.CurrentCell.RowIndex);
            temp[dataGridView_item.CurrentCell.RowIndex] = "< NAME >";

            Array.Copy(fieldNames[comboBox_ConfigLists.SelectedIndex], dataGridView_item.CurrentCell.RowIndex,
                temp, dataGridView_item.CurrentCell.RowIndex + 1,
                fieldNames[comboBox_ConfigLists.SelectedIndex].Length - dataGridView_item.CurrentCell.RowIndex);
            fieldNames[comboBox_ConfigLists.SelectedIndex] = temp;

            temp = new string[fieldTypes[comboBox_ConfigLists.SelectedIndex].Length + 1];

            Array.Copy(fieldTypes[comboBox_ConfigLists.SelectedIndex], 0, temp, 0, dataGridView_item.CurrentCell.RowIndex);
            temp[dataGridView_item.CurrentCell.RowIndex] = "< TYPE >";

            Array.Copy(fieldTypes[comboBox_ConfigLists.SelectedIndex], dataGridView_item.CurrentCell.RowIndex, temp,
                dataGridView_item.CurrentCell.RowIndex + 1,
                fieldTypes[comboBox_ConfigLists.SelectedIndex].Length - dataGridView_item.CurrentCell.RowIndex);
            fieldTypes[comboBox_ConfigLists.SelectedIndex] = temp;

            ChangeList(null, null);
        }

        private void DeleteRow(object sender, EventArgs e)
        {
            if (comboBox_ConfigLists.SelectedIndex <= -1) return;

            var temp = new string[fieldNames[comboBox_ConfigLists.SelectedIndex].Length - 1];

            Array.Copy(fieldNames[comboBox_ConfigLists.SelectedIndex], 0, temp, 0, dataGridView_item.CurrentCell.RowIndex);
            Array.Copy(fieldNames[comboBox_ConfigLists.SelectedIndex], dataGridView_item.CurrentCell.RowIndex + 1, temp,
                dataGridView_item.CurrentCell.RowIndex,
                fieldNames[comboBox_ConfigLists.SelectedIndex].Length - 1 - dataGridView_item.CurrentCell.RowIndex);

            fieldNames[comboBox_ConfigLists.SelectedIndex] = temp;

            temp = new string[fieldTypes[comboBox_ConfigLists.SelectedIndex].Length - 1];

            Array.Copy(fieldTypes[comboBox_ConfigLists.SelectedIndex], 0, temp, 0, dataGridView_item.CurrentCell.RowIndex);
            Array.Copy(fieldTypes[comboBox_ConfigLists.SelectedIndex], dataGridView_item.CurrentCell.RowIndex + 1, temp,
                dataGridView_item.CurrentCell.RowIndex,
                fieldTypes[comboBox_ConfigLists.SelectedIndex].Length - 1 - dataGridView_item.CurrentCell.RowIndex);

            fieldTypes[comboBox_ConfigLists.SelectedIndex] = temp;

            ChangeList(null, null);
        }
    }
}
